package event;

import java.sql.ResultSet;
import java.util.Hashtable;
import java.util.LinkedList;

import query.condition.Report_master;
import query.query.Condition;

import utility.Index;

import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;
import control.*;

public class DbIndex implements Event{

	private String TYPE="type";
	private String CONDITION="condition";
	private String TABLENAME="tablename";
	private String FIELDLIST="fieldlist";
	private String FIELD = "FIELD"; 
	private String USING="using";
	private String NAME="name";
	private String object;
	private String condition,tablename,fieldList,Using,name;
	String masterid;
	DBManager database;
	GetIdName gin;
	
	public String getTypeObj(String type)
	{
		this.TYPE=type;
		return this.TYPE;
	}
	
	public String getCondition(String conditionobject)
	{	
		Condition condition = new Condition();
		condition.setObject(conditionobject);
		condition.setDbmanager(database);
		condition.initializeData();
		Report_master report =new Report_master();
		report.setConditionMap(condition.getConditionVector());
		report.initializeData();
		report.setQueryCount(-99);
		report.setQuery(" ");
		report.getResult();
		return report.getQuery();
	}
	
	public String getName(String nameString)
	{
		LinkedList< Object> columnlist = new LinkedList<Object>();
		int a=0,b=0;
		String s;
		for(int i=0;i<nameString.length();i++)
		{
			a=Index.indexOf(nameString,"^", b+1);
			if(a>0) 
			{
				b=Index.indexOf(nameString,"^", a+1);
				s=nameString.substring(a+1,b);
				System.out.println("   "+s);
				columnlist.add(s);
				i=b;
			}
			else
				break;
		}
			
		try
		{
			Hashtable<Object, Object> hash = new Hashtable<Object, Object>();
			hash.put("mid",masterid);
			hash.put("td","null");
			RecordSearch rs = new RecordSearch(database);
			rs.setConditionMap(hash);
			rs.setTablename("property_details");
			ResultSet result = rs.getResultSet();
			result.next();
			for(int i=0;i<columnlist.size();i++)
			{
				String col = columnlist.get(i).toString();
				String value = result.getString(col);
				nameString = nameString.replace("^"+col+"^", value);
			}
			
	
	   }//try
		catch(Exception e)
		{
			//e.printStackTrace();
			System.out.println("problem in parsing............!!!!");
			System.out.println(e);
		}
		System.out.println(nameString);
		return nameString;
	}//getName()
	
	
	public String getFieldList(String fieldListObj)
	{
		FIELD = gin.getId(FIELD);
		Hashtable<Object, Object> hash = new Hashtable<Object, Object> ();
		hash.put("mid", fieldListObj);
		hash.put("pid", FIELD);
		hash.put("td", "null");
		
		RecordSearch rs = new RecordSearch(database);
		rs.setConditionMap(hash);
		rs.setTablename("property_details");
		String[] resultArray = rs.getArrayFromResultSet();
		//the resultset should be Sort on reference
		fieldListObj = "";
		for(int i=0; i<resultArray.length ; i++)
		{
			String[] resultString = resultArray[i].split(utility.Input.SEPARATOR);
			String field = resultString[Column.pv_index-1];
			if(field.equalsIgnoreCase("0"))
				field = resultString[Column.vt_index-1];
			else
				field = gin.getItem(field);
			fieldListObj = field+","+fieldListObj; 
		}
		fieldListObj = fieldListObj.substring(0, fieldListObj.length()-1);
		return fieldListObj;
	}
	
	

	
	public String getTableName(String tablename)
	{
		this.TABLENAME=tablename;
		return this.TABLENAME;
	}
	

	
	public void init()
	{
		CONDITION = gin.getId(CONDITION);
		FIELDLIST = gin.getId(FIELDLIST);
		TABLENAME = gin.getId(TABLENAME);
		USING = gin.getId(USING);
		NAME = gin.getId(NAME);
		
		Hashtable<Object, Object> hash = new Hashtable<Object, Object>();
		hash.put("mid", object);
		hash.put("td", "null");
		
		RecordSearch rs = new RecordSearch(database);
		rs.setConditionMap(hash);
		rs.setTablename("property_details");
		String []result =  rs.getArrayFromResultSet();
		hash.clear();
		for(int i=0; i<result.length;i++)
		{
			String[] resultString = result[i].split(utility.Input.SEPARATOR);
			String key = resultString[Column.pid_index-1];
			String value = resultString[Column.pv_index-1];
			if(value.equalsIgnoreCase("0"))
				value = resultString[Column.vt_index-1];
			hash.put(key, value);
		}
		try 
		{
			condition = getCondition(hash.get(CONDITION).toString());
		} catch (Exception e) {
		}
		fieldList =	getFieldList(hash.get(FIELDLIST).toString()); //getting exception here whencalling afterUpdate()
		name = getName(hash.get(NAME).toString());
		Using = hash.get(USING).toString();
		tablename = hash.get(TABLENAME).toString();
	}
	public void SetData(String Product, String type, String masterid,
			String userid, String Process) {
		
		this.masterid = masterid;
	}


	void initIndexProperty(String indexObject)
	{
		
	}
	
	public boolean afterAdd() 
	{
		String query;
		if(condition != null)
			query="CREATE INDEX "+name+ " ON "+tablename+" WHERE "+condition+" USING "+Using+" ("+fieldList+")";
		else
			query="CREATE INDEX "+name+ " ON "+tablename+" USING "+Using+" ("+fieldList+")";
		database.getInsert(query);
		return true;
	}

	public boolean afterDel() {
		String query="";
		if(name != null)
		{
			query="DROP INDEX "+name+";";
	        database.getInsert(query);
	        return true;
		}    
		return false;
	}

	public boolean afterMod() {
	
		if(afterDel())
		{
			System.out.println("afterDel is successful!");
			afterAdd();
		}
		return true;
	}


	public boolean afterView() {
		
		return false;
	}

	
	public void initializationData(DBManager database) 
	{
		this.database = database;
		gin = new GetIdName(database);
		
		TYPE= gin.getId(TYPE);
	}

	
	public void setObject(String Object) 
	{
		this.object = Object;
		init();
	}

	public static void main(String[] args) 
	{
		DBManager db = new DBManager();
		db.getConnect();
		DbIndex ob=new DbIndex();
		ob.initializationData(db);
		//System.out.println(ob.getCondition("7268"));
			/*//ob.getFieldList("2600001");
		ob.SetData("", "2600005", "2600006", "", "add");
		ob.setObject("2600006");
		//ob.afterAdd();
		//System.out.println("No w we will go for drp index!");
		//ob.afterDel();
<<<<<<< .mine
		ob.afterMod();
=======
		ob.afterupdate();*/

		//ob.getName("pid_^mid^_indx");
			
		
	}
	
	
	
}//class
